package com.wjjun.base.widget.decoration

import android.content.Context
import android.graphics.Canvas
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import com.wjjun.base.ui.viewmodel.BaseItemViewModel
import com.wjjun.base.ui.viewmodel.FloatItemViewModel
import me.tatarka.bindingcollectionadapter2.BindingRecyclerViewAdapter
import java.util.*
import kotlin.collections.HashMap


/**
 * 支持item吸附的Decoration
 */
class FloatItemDecoration(context: Context) :
    ViewModelItemDecoration(context) {

    //浮动View
    private var floatContent: ViewGroup? = null
    private val floatView = HashMap<Int, View>()
    private val floatViewRes = HashMap<Int, Int>()
    private var floatViewModelCache = HashMap<Int, BaseItemViewModel<*>>()

    /**
     * 设置float的容器
     */
    fun setFloatContent(view: ViewGroup?) {
        floatContent = view
    }

    override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        if (floatContent == null || parent.childCount <= 0) {
            return
        }
        if (parent.adapter == null || parent.adapter !is BindingRecyclerViewAdapter<*>) {
            return
        }

        val adapter = parent.adapter as BindingRecyclerViewAdapter<*>
        val firstView = parent.getChildAt(0)
        val index = parent.getChildAdapterPosition(firstView)
        if (index < 0) { //未找到
            return
        }

        //要添加的类型
        val floatViewTypes: MutableList<Int> = ArrayList()
        val floatViewModels = HashMap<Int, BaseItemViewModel<*>>()
        for (i in 0..index) {
            val itemViewModel = adapter.getAdapterItem(i)
            if (itemViewModel !is FloatItemViewModel<*>) {
                continue
            }
            if (!itemViewModel.isFloat()) {
                continue
            }
            //浮动类型，同类型的只能显示一个
            val floatType = itemViewModel.getFloatType()

            //新的类型或者同类型的不同布局
            if (!floatViewRes.containsKey(floatType) || itemViewModel.getFloatLayoutId() != floatViewRes[floatType]) {
                val binding: ViewDataBinding = DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context),
                    itemViewModel.getFloatLayoutId(),
                    null,
                    false
                )
                floatViewRes[floatType] = itemViewModel.getFloatLayoutId()
                floatView[floatType] = binding.root
            }
            //添加
            if (!floatViewTypes.contains(floatType)) {
                floatViewTypes.add(floatType)
            }
            floatViewModels[floatType] = itemViewModel
        }

        if (floatViewTypes.isEmpty()) { //在当前view前面没有可以浮动的view，则清空
            floatContent?.removeAllViews()
        } else { //更新浮动的控件
            if (floatViewModelCache === floatViewModels) {
                return
            }
            floatContent?.removeAllViews()
            for (type in floatViewTypes) {
                val view = floatView[type]
                if (view != null) {
                    val binding = DataBindingUtil.getBinding<ViewDataBinding>(view) ?: continue
                    adapter.itemBinding.bind(binding, floatViewModels[type] as Nothing?)
                    floatContent!!.addView(view)
                }
            }
            floatViewModelCache = floatViewModels
        }
    }

}
